home *** CD-ROM | disk | FTP | other *** search
/ boe.pres.k12.wv.us / boe.pres.k12.wv.us.zip / boe.pres.k12.wv.us / Utilities / Xerox Workcentre 5335 / Windows Scan / 64-bit_x64 / Russian / cpsimage.cab / data / sys / zip.elf < prev   
Text File  |  2009-04-23  |  10KB  |  256 lines

  1. /*
  2. ** $Id: zip.elf,v 1.8 2009/04/22 14:42:51 campanel Exp $
  3. */
  4.  
  5. #load "sys/stdio.elf";
  6.  
  7. /******************************************************************************/
  8. /*
  9. ** This class provides access to creating zip files.
  10. */
  11. /* @New
  12. // DESCRIPTION
  13.   Creates a new instance of ZIP for creating a zip archive.
  14.  
  15. // ARGUMENTS
  16.   FILE    file       The output zip file.
  17.   STRING  encoding   The encoding of the file names. The default is UTF-8.
  18.   BOOLEAN append     Files are added to an existing archive, otherwise the output is overwritten.
  19.  
  20. // EXAMPLE's
  21.   #load "sys/zip.elf";
  22.  
  23.   //-------------------------------------------------------
  24.   FILE zipfile = new (FILE, path: "output.zip");
  25.   ZIP zip = new (ZIP, file:zipfile);
  26.  
  27.   FILE f;           // This is the same as FILE f = new ();
  28.   LIST files = f.list();   // List the contents of the current directory
  29.  
  30.   zip.addFiles( dir:f, list:files );    // Add all files from the current dir
  31.   zip.close();
  32.  
  33.   //-------------------------------------------------------
  34.   zipfile = new (FILE, path: "other.zip");
  35.   zip = new (ZIP, file:zipfile);
  36.  
  37.   f = new (FILE, path:"datadir");
  38.   zip.addFiles( dir:f, recurse:TRUE );
  39.   zip.close();
  40.  
  41.   //-------------------------------------------------------
  42.   zipfile = new (FILE, path:"other.zip");
  43.   zip = new (ZIP, file:zipfile);
  44.  
  45.   zip.setOption( option:ZIP.NODIRS ); // Don't store directory references
  46.   FILE f = new (FILE, path:"doc");    // Store all of files in the doc dir
  47.   zip.addFiles( dir:f, recurse:TRUE );     // recurse down also
  48.   zip.close();
  49. */
  50. /* @addFile Adds a single file to the archive. The file is stored relative to
  51.     the given directory, therefore the file should exists below the given
  52.     directory. If no directory is specified then it is placed in the top level, 
  53.     i.e., "." */
  54. /* @addFiles Adds a directory of files to the archive. If a list is given then
  55.     those files are added. Otherwise all files in the directory are stored. If
  56.     recurse is set to true then all directories in the list or directory will be
  57.     recursed and added. All files are stored relative to the given directory. */
  58. /* @close Close the zip file. No further additions are possible. */
  59. /* @setOption Sets a compression option. */
  60. /******************************************************************************/
  61. CLASS ZIP {
  62.     METHOD New( FILE file, STRING encoding, BOOLEAN append ) NATIVE "ZipMethods@builtins";
  63.     METHOD Empty() RETURNS( BOOLEAN empty ) NATIVE "ZipMethods@builtins";
  64.     METHOD Free() NATIVE "ZipMethods@builtins";
  65.  
  66.     /* Methods */
  67.     METHOD addFile( FILE dir, FILE file, STRING comment ) RETURNS( BOOLEAN success ) NATIVE "ZipMethods@builtins";
  68.  
  69.     METHOD addFiles( FILE dir, LIST list, BOOLEAN recurse ) RETURNS( BOOLEAN success )
  70.     {
  71.         if( !list ) {
  72.             list = dir.listFiles();
  73.         }
  74.  
  75.         INTEGER i;
  76.         FILE f;
  77.         LIST sublist;
  78.  
  79.         success = TRUE;
  80.  
  81.         /* sort it to give a predictable order */
  82.         list.sort( comparator:this );
  83.  
  84.         for( i = 0; i < list.length(); i++ ) {
  85.             f = list[i];
  86.  
  87.             if( f.isDirectory() ) {
  88.                 success = this.addFile( dir:dir, file:f ); // The addFile will skip this if we are not storing dirs.
  89.                 if( !success ) {
  90.                     return;
  91.                 }
  92.                 sublist = f.listFiles();
  93.                 if( recurse && sublist ) {
  94.                     success = this.addFiles( dir:dir, list:sublist, recurse:recurse );
  95.                     if( !success ) {
  96.                         return;
  97.                     }
  98.                 }
  99.             } else {
  100.                 success = this.addFile( dir:dir, file:f );
  101.                 if( !success ) {
  102.                     return;
  103.                 }
  104.             }
  105.         }
  106.     }
  107.  
  108.     METHOD close( STRING comment ) RETURNS( BOOLEAN success ) NATIVE "ZipMethods@builtins";
  109.  
  110.     STRING NODIRS = "nodirs";
  111.     STRING LEVEL = "level";
  112.     STRING NOTIME = "notime";
  113.     STRING STORE = "-1";
  114.  
  115.     METHOD setOption( STRING option, STRING value ) NATIVE "ZipMethods@builtins";
  116.  
  117.     private METHOD compare( FILE left, FILE rite ) RETURNS( INTEGER result )
  118.     {
  119.         STRING l = left.getPath();
  120.         STRING r = rite.getPath();
  121.  
  122.         if( l < r ) {
  123.             result = -1;
  124.         } else if( l == r ) {
  125.             result = 0;
  126.         } else {
  127.             result = 1;
  128.         }
  129.     }
  130. }
  131.  
  132.  
  133. /******************************************************************************/
  134. /*
  135. ** This class provides access to zip file entry.
  136. */
  137. /* @isDirectory Is the item in the archive a directory. */
  138. /* @isFile Is the item in the archive a file. */
  139. /* @lastModified Gets the last modification time of the given file in the archive. */
  140. /* @getSize Gets the size of the given file in the archive. */
  141. /* @getCompressedSize Gets the compressed size of the given file in the archive. */
  142. /* @getPath Gets the STRING path of the given file in the archive. */
  143. /* @getFile Gets the FILE object of the given file in the archive. */
  144. /* @getComment Gets the comment of the given file in the archive. */
  145. /* @getCRC Gets the CRC value of the given file in the archive. */
  146. /* @getMethod Gets the method used to store/compress the given file in the archive. */
  147. /******************************************************************************/
  148. CLASS ZIPENTRY {
  149.     METHOD Empty() RETURNS( BOOLEAN empty ) NATIVE "ZipEntryMethods@builtins";
  150.  
  151.     INTEGER STORE = -1;
  152.     INTEGER DEFLATE = 8;
  153.  
  154.     /* Methods */
  155.     METHOD isDirectory() RETURNS( BOOLEAN isdir ) NATIVE "ZipEntryMethods@builtins";
  156.     METHOD isFile() RETURNS( BOOLEAN isfile ) NATIVE "ZipEntryMethods@builtins";
  157.     METHOD lastModified() RETURNS( LONG time ) NATIVE "ZipEntryMethods@builtins";
  158.     METHOD getSize() RETURNS( LONG size ) NATIVE "ZipEntryMethods@builtins";
  159.     METHOD getCompressedSize() RETURNS( LONG size ) NATIVE "ZipEntryMethods@builtins";
  160.     METHOD getMethod() RETURNS( INTEGER method ) NATIVE "ZipEntryMethods@builtins";
  161.     METHOD getCRC() RETURNS( INTEGER crc ) NATIVE "ZipEntryMethods@builtins";
  162.     METHOD getPath() RETURNS( STRING path ) NATIVE "ZipEntryMethods@builtins";
  163.     METHOD getFile() RETURNS( FILE path ) NATIVE "ZipEntryMethods@builtins";
  164.     METHOD getComment() RETURNS( STRING comment ) NATIVE "ZipEntryMethods@builtins";
  165. }
  166.  
  167.  
  168. /******************************************************************************/
  169. /*
  170. ** This class provides access to reading zip files.
  171. */
  172. /* @New
  173. // DESCRIPTION
  174.   Creates a new instance of UNZIP for reading a zip archive.
  175.  
  176. // ARGUMENTS
  177.   FILE    file       The input zip file.
  178.   STRING  encoding   The encoding of the file names. The default is UTF-8.
  179.  
  180. // EXAMPLE's
  181.   #load "sys/zip.elf";
  182.  
  183.   FILE zipfile = new (FILE, path: "output.zip");
  184.   ZIP zip = new (UNZIP, file:zipfile);
  185.  
  186.   FILE f = FILE.createTempDirectory();
  187.   zip.extractFiles( dir:f );
  188.  
  189.   zip.close();
  190.  
  191. // NOTE
  192.   A zip archive can be read in two ways, one involves seeking in the archive and
  193.   the other does not. If one wishes to read an archive in order with no seeking
  194.   then each entry can be read in order using the getNextEntry call. Each entry
  195.   must be read then extracted, or skipped, in order. At any time this mode of
  196.   access can be abandoned by either extracting a previous entry out of order or
  197.   by calling readCentralDirectory.
  198.  
  199.   The other way is to call readCentralDirectory to load the entire table of
  200.   contents for the archive. After this any entry may be accessed and/or extracted
  201.   in any order.
  202.  
  203.   Prior to using either of these methods there are no entries known to this
  204.   object, since none have been loaded. Therefore one or the other must be used.
  205.  
  206. */
  207. /* @readCentralDirectory Reads the entire table of contents of the archive. */
  208. /* @getNextEntry Gets the next entry for a archive being read linearly. */
  209. /* @getEntry Gets the entry for a given item in the archive. */
  210. /* @getEntryCount Gets the count of items in the archive. */
  211. /* @extractFiles Extracts all files into the given output directory. */
  212. /* @extractFile Extracts a single file into the given output directory with the given name. */
  213. /* @close Close the zip file. No further accesses are possible. */
  214. /* @setOption Sets an option. */
  215. /******************************************************************************/
  216. CLASS UNZIP {
  217.     METHOD New( FILE file, STRING encoding ) NATIVE "UnZipMethods@builtins";
  218.     METHOD Empty() RETURNS( BOOLEAN empty ) NATIVE "UnZipMethods@builtins";
  219.     METHOD Free() NATIVE "UnZipMethods@builtins";
  220.  
  221.     /* Methods */
  222.     METHOD readCentralDirectory() RETURNS( BOOLEAN success ) NATIVE "UnZipMethods@builtins";
  223.     METHOD getNextEntry() RETURNS( ZIPENTRY entry ) NATIVE "UnZipMethods@builtins";
  224.  
  225.     METHOD getEntryCount() RETURNS( INTEGER count ) NATIVE "UnZipMethods@builtins";
  226.     METHOD getEntry( INTEGER index ) RETURNS( ZIPENTRY entry ) NATIVE "UnZipMethods@builtins";
  227.  
  228.     METHOD extractFile( ZIPENTRY entry, FILE dir, FILE file ) RETURNS( BOOLEAN success ) NATIVE "UnZipMethods@builtins";
  229.  
  230.     METHOD extractFiles( FILE dir ) RETURNS( BOOLEAN success )
  231.     {
  232.         INTEGER i, count;
  233.         ZIPENTRY entry;
  234.  
  235.         this.readCentralDirectory();
  236.         count = this.getEntryCount();
  237.  
  238.         for( i = 0; i < count; i++ ) {
  239.             entry = this.getEntry( index:i );
  240.             if( !entry ) {
  241.                 success = FALSE;
  242.                 return;
  243.             }
  244.  
  245.             success = this.extractFile( entry:entry, dir:dir, file:entry.getFile() );
  246.             if( !success ) {
  247.                 return;
  248.             }
  249.         }
  250.     }
  251.  
  252.     METHOD close() RETURNS( BOOLEAN success ) NATIVE "UnZipMethods@builtins";
  253.  
  254.     METHOD setOption( STRING option, STRING value ) NATIVE "UnZipMethods@builtins";
  255. }
  256.